package com.gl.rent;

import com.gl.common.util.AliyunOssUtil;
import com.haixiaoke.saas.repository.core.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求 Controller
 *
 * @author zhiping
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    /**
     * 通用上传
     *
     * @param file
     * @return
     */
    @PostMapping("/oss/upload")
    public AjaxResult aliyunUploadFile(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            // 上传并返回新文件名称
            String url = aliyunOssUtil.uploadFileToOss(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
